﻿Public Class FormComprasListado

    Private Sub FormComprasListado_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgCompras.AutoGenerateColumns = False
        fechaInicio.Value = DateTime.Now.AddDays(-5)

        CargarCombos()
        CargarCompras()
    End Sub

    Dim classController As New Class_RC_Controller.rc_Facturas()
    Dim clientes As New Class_RC_Controller.rc_Proveedores

    Private m_frmCompra As FrmCompras
    Public Property frmCompras() As FrmCompras
        Get
            Return m_frmCompra
        End Get
        Set(value As FrmCompras)
            m_frmCompra = value
        End Set
    End Property

    Private Sub CargarCompras()

        Dim tipoCompra As String = ""
        If cbxTipo.Checked Then
            tipoCompra = cboTipo.Text
        End If

        Dim estadoCompra As String = ""
        If cbxEstado.Checked Then
            estadoCompra = cboEstado.Text
        End If

        Dim dt As New DataTable
        dt = classController.get_Compras_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, tipoCompra, txtNombre.Text,
        estadoCompra, txtNro.Text, txtRuc.Text, 0, False)
        dgCompras.DataSource = dt

        If Not classController.MessageError Is Nothing Then
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub CargarCombos()

        cboEstado.SelectedIndex = 0
        cboTipo.SelectedIndex = 0

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(sender As System.Object, e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgCompras.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgCompras.SelectedRows(0).Cells(0).Value

                If frmCompras Is Nothing Then

                    frmCompras = New FrmCompras()
                    frmCompras.codCompra = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmCompras.ShowDialog()
                    CargarCompras()
                    frmCompras.Dispose()
                    frmCompras = Nothing
                    'End If
                Else
                    frmCompras.codCompra = cod
                    frmCompras.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarCompras()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCompras()

        End If
    End Sub

    Private Sub dgVentas_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgCompras.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgVentas_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgCompras.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub



    Private Sub txtRuc_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtRuc.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCompras()

        End If
    End Sub

    Private Sub txtNro_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNro.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCompras()

        End If
    End Sub


    Private Sub cbxTipo_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxTipo.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCompras()

        End If
    End Sub



    Private Sub cboTipo_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboTipo.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCompras()

        End If
    End Sub

    Private Sub cbxEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCompras()

        End If
    End Sub

    Private Sub cboEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarCompras()

        End If
    End Sub

    Private m_frmRpts As FormReportes
    Public Property frmRpts() As FormReportes
        Get
            Return m_frmRpts
        End Get
        Set(value As FormReportes)
            m_frmRpts = value
        End Set
    End Property

    Private Sub btnImprimir_Click(sender As System.Object, e As System.EventArgs) Handles btnImprimir.Click

        If frmRpts Is Nothing Then
            frmRpts = New FormReportes()
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "COMPRAS"
            frmRpts.datos_Reporte = CType(dgCompras.DataSource, DataTable).Copy
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New FormReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "COMPRAS"
                frmRpts.datos_Reporte = CType(dgCompras.DataSource, DataTable).Copy
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "COMPRAS"
                frmRpts.datos_Reporte = CType(dgCompras.DataSource, DataTable).Copy
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try

            Dim dt As New DataTable
            dt = classController.get_Compras_Mes(oiml.dbConn, fechaInicio.Value.Year, fechaFin.Value.Month)

            If Not dt Is Nothing Then

                If frmRpts Is Nothing Then
                    frmRpts = New FormReportes()
                    frmRpts.tipo_Reporte = "COMPRASMES"
                    frmRpts.datos_Reporte = dt
                    frmRpts.ShowDialog()
                Else

                    If Not frmRpts.Visible Then
                        frmRpts = New FormReportes()
                        frmRpts.tipo_Reporte = "COMPRASMES"
                        frmRpts.datos_Reporte = dt
                        frmRpts.ShowDialog()
                    Else
                        frmRpts.tipo_Reporte = "COMPRASMES"
                        frmRpts.datos_Reporte = dt
                        frmRpts.Select()
                        frmRpts.Activate()
                    End If

                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

End Class